﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vukhoas.src.POJO;
using System.Data.SqlClient;
using vukhoas.src;
using System.IO;

namespace vukhoas.VukhoasAdmin
{
    public partial class NewProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private bool insertProduct() 
        {
            bool flag = false;
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "INSERT INTO product VALUES(@name,@price,@manu,@cate,@detail,@intro,@info,@img,@date)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@name",name.Text);
                cmd.Parameters.AddWithValue("@price", price.Text);
                cmd.Parameters.AddWithValue("@manu", manu.SelectedValue);
                cmd.Parameters.AddWithValue("@cate", cate.SelectedValue);
                cmd.Parameters.AddWithValue("@detail", detail.Text);
                cmd.Parameters.AddWithValue("@intro", intro.Text);
                cmd.Parameters.AddWithValue("@info", info.Text);
                cmd.Parameters.AddWithValue("@img", "/data/upload/"+img.Text+"/");
                cmd.Parameters.AddWithValue("@date",DateTime.Now);
                cmd.ExecuteNonQuery();
                flag = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return flag;
        }

        protected List<Category> lstCategory(){
            List<Category> lst = new List<Category>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM category";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Category cate = new Category();
                    cate.Id=dr.GetString(0);
                    cate.Name = dr.GetString(1);
                    cate.Description = dr.GetString(2);
                    lst.Add(cate);
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }

        protected List<Manufacture> lstManufacture()
        {
            List<Manufacture> lst = new List<Manufacture>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM manufacture";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Manufacture cate = new Manufacture();
                    cate.Id = dr.GetString(0);
                    cate.Name = dr.GetString(1);
                    cate.Description = dr.GetString(2);
                    lst.Add(cate);
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }

        protected void ok_Click(object sender, EventArgs e)
        {
            if (insertProduct())
            {
                Alert.Show("Sản phẩm đã được tạo mới thành công");
                Response.Redirect("ListProduct.aspx");
            }
            else
            {
                Alert.Show("Có lỗi! Vui lòng kiểm tra lại");
            }            

        }

        protected void reset_Click(object sender, EventArgs e)
        {
            
        }

        protected void cate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string file = "/data/template/";
            switch (cate.SelectedValue)
            { 
                case "macbook":
                    file += "laptop.htm";
                    break;
                case "iphone":
                    file += "phone.htm";
                    break;
                case "smartphone":
                    file += "phone.htm";
                    break;
                case "phd":
                    file += "phd.htm";
                    break;
                case "ipad":
                    file += "tablet.htm";
                    break;
            }
            StreamReader sr = File.OpenText(Server.MapPath(file));
            detail.Text = sr.ReadToEnd();
        }
    }
}